package logic

import (
	"context"

	"mydemo/rpc/user/internal/model"
	"mydemo/rpc/user/internal/svc"
	"mydemo/rpc/user/types/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic {
	return &GetUserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserListLogic) GetUserList(in *user.GetUserListInput) (*user.GetUserListOutput, error) {
	// todo: add your logic here and delete this line
	out := &user.GetUserListOutput{}
	userList, err := (&model.User{}).List(l.svcCtx.Db)
	if err != nil {
		return out, err
	}
	for _, v := range userList {
		out.List = append(out.List, &user.GetUserList{
			Id:       uint32(v.Id),
			Name:     v.Name,
			PassWord: v.Password,
			Gender:   v.Gender,
		})
	}
	l.svcCtx.Redis.SetexCtx(l.ctx, "zero-rpc-user", out.List[0].Name, 120)
	out.Total = uint32(len(out.List))
	return out, nil
}
